<?php
/**
 * 会员管理
 * @author yangzhiguo 2011-04-30
 */
defined('IN_LK') or die('Access Denied');
lk::lo_app('member_common', 'member', 0);
class member extends member_common
{
	private $member_db = null;
	private $group_db  = null;
	
	public function __construct()
	{
		parent::__construct();
		lk::lo('form', '', 0);
		$this->member_db = lk::load_model('member');
		$this->group_db  = lk::load_model('member_group');
	}
	
	/**
	 * 管理员后台管理会员
	 */
	public function manage()
	{
		$_GET['page'] = isset($_GET['page']) ? (int)$_GET['page'] : 1;
		$members = $this->member_db->listinfo('*', '', $_GET['page'], 25, '`userid` DESC');
		$page_nav = $this->member_db->page_nav;
		include self::admin_t('member_list');
	}
	
	/**
	 * 管理员添加会员
	 */
	public function add()
	{
		if(isset($_POST['dosubmit']))
		{
			$info = $_POST['info'];
			if($info['password'] != $info['password2'] 
			   || !is_password($info['password'])
			   || !is_username($info['nickname'])
			   || !is_username($info['username']) 
			   || !is_email($info['email']) 
			   || !isset($info['point'])
			   || !isset($info['groupid']))
			{
				showmsg(LANG('parameters_error'), false);
			}
			$data = in_fields($info, array('username', 'password', 'nickname',
			                               'email', 'groupid', 'point'));
			$data['password']  = password($data['password']);
			if($data['point'] != 0) : $data['groupid'] = $this->point2group($data['point']); endif;
			$data['regtime']   = TIME;
			if($this->member_db->insert($data) && $this->group_db->update(array('totalnum'=>'+=1'), "`groupid`='" . $data['groupid'] . "'"))
			{
				//todo 更新会员组缓存
				showmsg(LANG('update_success'), '', '', '', 'add_member');
			}
			else
			{
				showmsg(LANG('update_false'), false);
			}
		}
		else
		{
			$member_group = get_cache('grouplist');
			$group = array();
			foreach($member_group as $k => $m)
			{
				if($m['disabled']) : continue; endif;
				$group[$m['groupid']] = $m['name'];
			}
			$select = form::select($group, 'name="info[groupid]" size="1"', 1);
			include self::admin_t('member_add');
		}
	}
	
	/**
	 * 检查用户名是否存在
	 */
	public function check_username_ajax()
	{
		$username = isset($_GET['username']) ? $_GET['username'] : exit('0');
		$userid   = isset($_GET['userid'])   ? (int)$_GET['userid'] : 0;
		if(!is_username($username) || $this->is_exists('username', $username, $userid))
		{
			exit('0');
		}
		exit('1');
	}
	
	/**
	 * 检查邮箱是否存在
	 */
	public function check_email_ajax()
	{
		$email  = isset($_GET['email']) ? $_GET['email'] : exit('0');
		$userid = isset($_GET['userid'])   ? (int)$_GET['userid'] : 0;
		if(!is_email($email) || $this->is_exists('email', $email, $userid))
		{
			exit('0');
		}
		exit('1');
	}	
	
	/**
	 * 检查昵称是否存在
	 */
	public function check_nickname_ajax()
	{
		$nickname = isset($_GET['nickname']) ? $_GET['nickname'] : exit('0');
		$userid   = isset($_GET['userid'])   ? (int)$_GET['userid'] : 0;
		if(!is_username($nickname) || $this->is_exists('nickname', $nickname, $userid))
		{
			exit('0');
		}
		exit('1');
	}
	
	/**
	 * 检查某个字段是否存在重复
	 * @param  string $field   字段名
	 * @param  string $value   不允许重复的值
	 * @param  int    $allowid 允许重复的id（自己）
	 * @return bool   $r       存在返回true
	 */
	private function is_exists($field, $value, $allowid = 0)
	{
		$where  = "`$field`='$value'";
		$where .= $allowid ? " AND `userid`!='$allowid'" : '';
		return (boolean)$this->member_db->get_one('userid', $where);
	}
	
	/**
	 * 积分决定会员组
	 * @param  int $point   积分
	 * @return int $groupid 该积分所属的会员组
	 */
	private function point2group($point)
	{
		$groupid = $gpoint = $k = $m = null;
		$group = $member_group = array();
		$member_group = get_cache('grouplist');
		foreach($member_group as $k => $m)
		{
			if($m['disabled']) : continue; endif;
			$group[$m['groupid']] = $m['point'];
		}
		asort($group);
		foreach($group as $groupid => $gpoint)
		{
			if($point <=$gpoint)
			{
				break;
			}
		}
		return $groupid;
	}
}